﻿/*!
 * Onejs Panel Plugin Version 1.0.0
 * Date: 2012-05-16 17:45:11
 * Copyright © OneStudio, Inc. All Rights Reserved. 
 */

(function( $, onejs, window ){

var document = window.document,
    $window = $(window);
    count = 0,
    expando = 'PAN' + (+new Date),

onepanel = function()	
{
	return onejs.tool.clone( onejspanel );
},

onejspanel =
{
    dom: {}, curDOM: null,
	
	init: function( config )
	{
	    config = config || {};
		config.elms = config.elms || [{id:'onepanel',tpl:'default'}];
		
		onejs.tool.extend( config, onepanel.setting );
		
		var that = this, i = 0, id, tpl,
    		l = config.elms.length;
		
		for( ; i < l; i++ )
		{
		    id = config.elms[i].id || 'onepanel';
			tpl = config.elms[i].tpl || 'default';
			
			that.dom[id] = that._getDOM( onepanel.tpls[tpl] );
		}
			
		that.config = config;
		that.curDOM = that.dom[ config.elms[0].id ];
		
		return that;
	},
	
	_getDOM: function( tpl )
	{
		var wrap = $(tpl);
		wrap[0].style.cssText = 'position:absolute;left:0;top:0;display:none;';
		
        var name, i = 0,
			DOM = { wrap: wrap },
			els = wrap[0].getElementsByTagName('*'),
			len = els.length;
			
		for( ; i < len; i ++ )
		{
			name = els[i].className.split('oui_pl_')[1];
			if(name) DOM[name] = $(els[i]);
		}
		
		document.body.appendChild(wrap[0]);
		
		return DOM;
	},
	
	content: function( txt )
	{
	    var that = this,
		    dom = that.curDOM,
			txt = txt || '';
			
		dom.content.html( txt );
		
		return that;
	},
	
	follow: function( elem, position, offset )
	{
	    var that = this, x, y,
		    wrap = that.curDOM.wrap[0],
		    ps = position || ['left','bottom'],
			of = offset || [0,0],
			tw = wrap.offsetWidth,
			th = wrap.offsetHeight,
			ew = elem.offsetWidth,
			eh = elem.offsetHeight,
			dl = $window.scrollLeft(),
			dt = $window.scrollTop(),
			dw = $window.width(),
			dh = $window.height(), cr;
		
		if( elem === 'contextmenu' )
		{
		    x = ps[0] + of[0]; y = ps[1] + of[1];
			
			if( x + tw > dl + dw )
			    x = x - tw;
			
			if( y + th > dt + dh )
			    y = y - th;
		}
		else
		{
		    cr = onejs.tool.offset( elem, ps, of );
			x = cr.x;
		
			if( ps[1] === 'bottom' )
			{
				y = cr.y;
				
				if( ps[0] === 'right' )
					x = cr.x - tw;
				else if( ps[0] === 'center' )
					x = cr.x - tw/2;
				
				if( x + tw > dl + dw )
					x = x + ew - tw;
				
				if( y + th > dt + dh )
					y = y - eh - th;
				
				if( x < dl )
					x = cr.x - ew;
			}
			else if( ps[1] === 'top' )
			{
				y = cr.y - th;
				
				if( ps[0] === 'right' )
					x = cr.x - tw;
				else if( ps[0] === 'center' )
					x = cr.x - tw/2;
				
				if( x + tw > dl + dw )
					x = x + ew - tw;
					
				if( y < dt )
					y = cr.y + eh;
				
				if( x < dl )
				    x = cr.x - ew;
			}
			else if( ps[1] === 'center' )
			{
				y = cr.y - th/2;
				
				if( ps[0] === 'left' )
					x = cr.x - tw;
				
				if( x + tw > dl + dw )
					x = x - ew - tw;
				
				if( x < dl )
					x = cr.x + eh;
					
				if( y + th > dt + dh )
					y = dt + dh - th;
				
				if( y < dt )
					y = dt;
			}
		}
		
		that.curDOM.wrap.css({ left:x + 'px', top:y + 'px' });
		
		return that;
	},
	
	get: function( id )
	{
	    this.curDOM = this.dom[id];
		
		return this;
	},
	
	show: function()
	{
	    this.curDOM.wrap[0].style.display = 'block';
		
		return this;
	},
	
	hide: function()
	{
	    this.curDOM.wrap[0].style.display = 'none';
		
		return this;
	}
};

onepanel.tpls =
{
    'default':
	    '<div class="oui_pl_default">' +
			'<div class="oui_pl_content"></div>' +
			'<div class="oui_pl_trigon_border"></div>'+
			'<div class="oui_pl_trigon"></div>'+
		'</div>'
	
};

onepanel.settting =
{

};

onejs.plugin.panel = onepanel;

})( window.jQuery, window.onejs, window );